<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2024/1/13
 * Time: 10:13
 */

namespace app\controller\admin;


use think\facade\Db;

class Index extends Base
{
    public function index($name = 'index'){
        $count = Db::name('order')->where('status',1)->count();
        $total = Db::name('order')->where('status','<>',1)->count();
        return $this->view('/admin/'.$name,['count' => $count,'total' => $total]);
    }

    public function input($type,$field = ''){
        $info = cache($type);
        $id = input('id');
        if(empty($info) && $type == 'company')
            $info = [
                ['id' => 'company_name','name' => '客户公司'],
                ['id' => 'address','name' => '公司地址'],
                ['id' => 'nickname','name' => '询价联系人'],
                ['id' => 'mobile','name' => '联系方式']
            ];
        if(empty($info) && $type == 'my')
            $info = [
                ['id' => 'my_name','name' => '报价公司'],
                ['id' => 'my_address','name' => '公司地址'],
                ['id' => 'my_ca','name' => '业务联系人'],
                ['id' => 'my_mobile','name' => '联系方式'],
                ['id' => 'order_no','name' => '报价订单号'],
                ['id' => 'day','name' => '报价日期'],
                ['id' => 'date','name' => '报价有效期'],
                ['id' => 'delivery_date','name' => '交货日期'],
            ];
        $post = input('post.');
        if (!empty($post) && empty($field)){
            $count = count($info);
            $info[] = [
                'id' => 'other'.$count,
                'name' => $post['name']
            ];
            cache($type,$info);
            return $this->msg();
        }else if (!empty($field) && !empty($post)){
            $name = input('name');
            if ($field == 'save') {
                if (!empty($info)) {
                    foreach ($info AS $key => $value) {
                        if ($id == $value['id'])
                            $info[$key]['name'] = $name;
                    }
                }
            }else if($field == 'del'){
                if (!empty($info)) {
                    foreach ($info AS $key => $value) {
                        if ($id == $value['id'])
                            unset($info[$key]);
                    }
                }
            }else{
                return $this->msg();
            }
            cache($type,$info);
            return $this->msg();
        }else{
            $content = [];
            if(!empty($info)){
                foreach ($info AS $key => $value){
                    if ($id == $value['id'])
                        $content = $value;
                }
            }
            return $this->view('/admin/input',['content' => $content,'type' => $type,'field' => $field,'id' => $id]);
        }
    }

    public function company(){
        $info = cache('my');
        empty($info) && $info = [
            ['id' => 'my_name','name' => '报价公司'],
            ['id' => 'my_address','name' => '公司地址'],
            ['id' => 'my_ca','name' => '业务联系人'],
            ['id' => 'my_mobile','name' => '联系方式'],
            ['id' => 'order_no','name' => '报价订单号'],
            ['id' => 'day','name' => '报价日期'],
            ['id' => 'date','name' => '报价有效期'],
            ['id' => 'delivery_date','name' => '交货日期'],
        ];
        return $this->view('/admin/my/info',['info' => $info]);
    }
    public function material_value(){
        $save = input('post.');
        if(!empty($save)) {
            cache('material', json_encode($save));
            return $this->msg();
        }
        $material_value = json_decode('{
            "304": {
                "md": 7.93,
                "duanda_name": "260锤加工费",
                "duanda_amount": 2.7,
                "gurongjiao": 0.8,
                "amount": 0.5,
                "pale": 1.05,
                "loss": 0.15,
                "price": 12.06,
                "price2": 13.4,
                "geban": 13.5,
                "juanban": 14.2,
                "feiliao_amount": 9
            },
            "316L": {
                "md": 7.93,
                "duanda_name": "400气锤加工费",
                "duanda_amount": 3.2,
                "gurongjiao": 0.8,
                "amount": 0.55,
                "pale": 1.065,
                "loss": 0.2,
                "price": 21.51,
                "price2": 23.9,
                "geban": 23.7,
                "juanban": 24.4,
                "feiliao_amount": 16.5
            },
            "2205": {
                "md": 7.93,
                "duanda_name": "皮带锤加工费",
                "duanda_amount": 3.5,
                "gurongjiao": 1,
                "amount": 0.9,
                "pale": 1.07,
                "loss": 0.23,
                "price": 23.04,
                "price2": 25.6,
                "geban": 26.5,
                "juanban": 27,
                "feiliao_amount": 16.5
            },
            "2507": {
                "md": 8.03,
                "duanda_name": "碾环加工费",
                "duanda_amount": 1.8,
                "gurongjiao": 1.5,
                "amount": 1.2,
                "pale": 1.15,
                "loss": 0.3,
                "price": 29.7,
                "price2": 33.0,
                "geban": 34,
                "juanban": 35,
                "feiliao_amount": 17.5
            }
        }');
        $info = json_decode(cache('material'));
        if (!empty($info))
            $material_value = $info;
        return $this->msg(['material_value' => $material_value]);
    }
}